package com.example.forumsystem.common;


import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * 统一返回格式
 */
public class AppResult<T> {

    // 强制进行序列化,不管是否为空
    // 状态码
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private int code;
    // 描述信息
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;
    // 返回的数据
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;

    public AppResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
    public AppResult(int code, String message) {
        this(code,message,null);
    }

    /**
     * 成功
     * @param
     * @return
     * @param <T>
     */
    public static <T> AppResult<T> success() {
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.massage);
    }
    public static <T> AppResult<T> success(String message) {
        return new AppResult(ResultCode.SUCCESS.getCode(), message);
    }
    public static <T> AppResult<T> success(T data) {
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.massage,data);
    }
    public static <T> AppResult<T> success(String message,T data) {
        return new AppResult(ResultCode.SUCCESS.getCode(), message,data);
    }

    /**
     * 失败
     * @return
      */
    public static <T> AppResult<T> failed() {
        return new AppResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.massage);
    }
    public static <T> AppResult<T> failed(String message) {
        return new AppResult(ResultCode.FAILED.getCode(), message);
    }
    public static <T> AppResult<T> failed(AppResult appResult) {
        return new AppResult(appResult.getCode(),appResult.getMessage());
    }

    public static AppResult failed(ResultCode errorCode) {
        return new AppResult(errorCode.getCode(), errorCode.massage);
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
